#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\experimental\commodities\commodity.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Experimental { namespace Commodities {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IPricingError
	public ref class CPricingError : 
            public System::MarshalByRefObject,
            public Cephei::QL::Experimental::Commodities::IPricingError
	{
	protected: 
		boost::shared_ptr<QuantLib::PricingError>* _ppPricingError;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::PricingError>* _phPricingError;
#endif
		Object^ _PricingErrorOwner;     // reference to object that manages the storage for this object
	internal:
		CPricingError (QL::Experimental::Commodities::PricingError::LevelEnum errorLevel, String^ error, String^ detail);
        CPricingError (boost::shared_ptr<QuantLib::PricingError>& childNative, Object^ owner);
        CPricingError (QuantLib::PricingError& childNative, Object^ owner);
        CPricingError (CPricingError^ copy);
        CPricingError (System::Type^ t);
#ifdef STRUCT
        CPricingError (QuantLib::PricingError childNative);
#endif       
#ifdef HANDLE
		CPricingError (QuantLib::Handle<QuantLib::PricingError>& childNative, Object^ owner);
		CPricingError (QuantLib::Handle<QuantLib::PricingError> childNative);
#endif
		virtual ~CPricingError ();
		!CPricingError ();

	internal:
		QuantLib::PricingError& GetReference ();
		boost::shared_ptr<QuantLib::PricingError>& GetShared ();
		QuantLib::PricingError* GetPointer ();
        void SetPricingError (boost::shared_ptr<QuantLib::PricingError> native)
        {
            if (_ppPricingError != NULL)
                delete _ppPricingError;
            _ppPricingError = new boost::shared_ptr<QuantLib::PricingError> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::PricingError>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IPricingError Vector interface
	ref class CPricingErrorVector : public System::MarshalByRefObject,  
							    public INativeVector<IPricingError^>,
							    public INativeVector<Generic::ICell<IPricingError^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::PricingError>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::PricingError>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::PricingError>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CPricingErrorVector ();
#ifdef STRUCT
		CPricingErrorVector (std::vector<QuantLib::PricingError>& vec, Object^ owner);
		CPricingErrorVector (std::vector<QuantLib::PricingError> vec);
#endif       
		CPricingErrorVector (std::vector<boost::shared_ptr<QuantLib::PricingError>>& vec, Object^ owner);
#ifdef HANDLE
		CPricingErrorVector (std::vector<QuantLib::Handle<QuantLib::PricingError>>& vec, Object^ owner);
#endif
		virtual ~CPricingErrorVector ();
		!CPricingErrorVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IPricingError^ default[int] 
		{
			virtual IPricingError^ get (int index) = INativeVector<IPricingError^>::default::get;
			virtual void set (int index, IPricingError^ value) = INativeVector<IPricingError^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Experimental::Commodities::IPricingError^>^ CItem[int] 
		{
			virtual Generic::ICell<IPricingError^>^ get (int index) = INativeVector<Generic::ICell<IPricingError^>^>::default::get;
			virtual void set (int index, Generic::ICell<IPricingError^>^ value) = INativeVector<Generic::ICell<IPricingError^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::PricingError>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::PricingError>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::PricingError>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CPricingError_Factory : public System::MarshalByRefObject,  public IPricingError_Factory
	{
	public:
        virtual IPricingError^ Create (QL::Experimental::Commodities::PricingError::LevelEnum errorLevel, String^ error, String^ detail);
        /* <summary> Create a hybrid Vector of IPricingError, with event notification of changes </summary> */
        virtual Cephei::IVector<IPricingError^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IPricingError, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IPricingError^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IPricingError^>^ CreateVector(System::Collections::Generic::IEnumerable<IPricingError^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IPricingError^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IPricingError^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Experimental*/ } /*Commodities */}
